<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="单据号" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入单据号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物流单号" prop="logisticsNo">
        <el-input
          v-model="queryParams.logisticsNo"
          placeholder="请输入物流单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物流公司" prop="logisticsCompanyId">
        <el-select v-model="queryParams.logisticsCompanyId" placeholder="全部" clearable :style="{width: '100%'}">
          <el-option v-for="(item, index) in logisticsCompany" :key="index" :label="item.name"
                     :value="item.id" ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="发货人" prop="deliveryBy">
        <el-select v-model="queryParams.deliveryBy" placeholder="全部" clearable :style="{width: '100%'}">
          <el-option v-for="(item, index) in user" :key="index" :label="item.name"
                     :value="item.id" ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="发货时间" prop="deliveryTime">
        <el-date-picker clearable
                        v-model="queryParams.deliveryMinTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        :style="{width: '45%'}">
        </el-date-picker>
        <span style="margin: 0 10px">至</span>
        <el-date-picker clearable
                        v-model="queryParams.deliveryMaxTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        :style="{width: '45%'}">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="操作时间" prop="createTime">
        <el-date-picker clearable
                        v-model="queryParams.createMinTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        :style="{width: '45%'}">
        </el-date-picker>
        <span style="margin: 0 10px">至</span>
        <el-date-picker clearable
                        v-model="queryParams.createMaxTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        :style="{width: '45%'}">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="操作人" prop="createBy">
        <el-select v-model="queryParams.createBy" placeholder="全部" clearable :style="{width: '100%'}">
          <el-option v-for="(item, index) in user" :key="index" :label="item.name"
                     :value="item.id" ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:logistics:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:logistics:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:logistics:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:logistics:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="logisticsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="单据号" align="center" prop="code" width="180"/>
      <el-table-column label="物流单号" align="center" prop="logisticsNo" />
      <el-table-column label="物流公司" align="center" prop="logisticsCompanyName" width="150"/>
      <el-table-column label="总重量" align="center" prop="totalWeight" />
      <el-table-column label="总体积" align="center" prop="totalVolume" />
      <el-table-column label="物流费" align="center" prop="totalAmount" />
      <el-table-column label="操作时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作人" align="center" prop="createBy" width="100"/>
      <el-table-column label="状态" align="center" prop="status" >
        <template slot-scope="scope">
          <span>{{ scope.row.status === 0 ? '待发货' : '已发货' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发货时间" align="center" prop="deliveryTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deliveryTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发货人" align="center" prop="deliveryBy" width="100"/>
      <el-table-column label="备注" align="center" prop="description" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="200">
        <template slot-scope="scope">
          <el-button
            size="medium"
            type="text"
            @click="selectPurchaseReturn(scope.row)"
            v-hasPermi="['system:PurchaseOrder:edit']"
          >查看</el-button>
          <el-button
            size="medium"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:PurchaseOrder:edit']"
          >修改</el-button>
          <el-button
            size="medium"
            type="text"
            @click="statusUpdate(scope.row)"
          >发货</el-button>
          <el-button
            size="medium"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:PurchaseOrder:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


    <!-- 查看按钮 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <div style="border: 1px gainsboro solid;padding: 10px 10px 10px 30px">
        <div style="display: inline-block;width: 300px;"><p>单据号：{{ this.form.code }}</p></div>
<!--        <div style="display: inline-block;width: 300px;"><p>状态：<span :style="{color: statusColor}">{{ this.form.status==0 ? '待审核' : (this.form.status==3 ? '审核通过' : '审核拒绝') }}</span></p></div>-->
        <br>
        <div style="display: inline-block;width: 300px;"><p>操作人：{{ this.form.createBy }}</p></div>
        <div style="display: inline-block;width: 300px;"><p>操作时间：{{ this.form.createTime }}</p></div>
      </div>
      <div style="border: 1px gainsboro solid;margin-top: 5px">
        <el-table height="400px" v-loading="loading" :data="this.tblPurchaseReturnDetailList" @selection-change="handleSelectionChange">
          <el-table-column label="业务单据号" align="left" prop="productInfo.id" width="200px"/>
          <el-table-column label="业务类型" align="left" prop="isGift" width="180">
            <template slot-scope="scope">
              <span>{{ scope.row.isGift == 0 ? '否' : '是' }}</span>
            </template>
          </el-table-column>
          <el-table-column label="仓库名称" align="left" prop="productInfo.name" width="200px"/>
          <el-table-column label="收件人" align="left" prop="productInfo.skuCode" width="100px"/>
          <el-table-column label="操作时间" align="left" prop="productInfo.externalCode" width="100px"/>
          <el-table-column label="操作人" align="left" prop="productInfo.unit" width="100px"/>
        </el-table>
      </div>
      <div style="border: 1px gainsboro solid;margin-top: 5px;padding: 10px">
        <h3 style="margin:0 0 15px 10px;font-weight: bolder;">合计</h3>
        <div style="display: inline-block;margin-left:50px">
          退货数量：<input type="text" style="background-color: #FFFFFF;height: 30px;width: 120px;border-radius: 5px;margin-left: 10px;border: 1px gainsboro solid" disabled>
        </div>
        <div style="display: inline-block;margin-left:50px">
          赠品数量：<input type="text" style="background-color: #FFFFFF;height: 30px;width: 120px;border-radius: 5px;margin-left: 10px;border: 1px gainsboro solid" disabled>
        </div>
        <div style="display: inline-block;margin-left:50px">
          含税总金额：<input type="text" style="background-color: #FFFFFF;height: 30px;width: 120px;border-radius: 5px;margin-left: 10px;border: 1px gainsboro solid" disabled>
        </div>
      </div>
      <div style="border: 1px gainsboro solid;margin-top: 5px;padding: 10px;text-align: right">
        <label >备注： </label>
        <textarea style="width: 90%;height: 50px;margin-top: 10px;margin-left: 10px;margin-bottom: 10px;border-radius: 5px;vertical-align:middle;border: 1px gainsboro solid" disabled/>
      </div>
    </el-dialog>

    <!-- 拒绝原因 -->
<!--    <el-dialog :title="title" :visible.sync="refuseOpen" width="500px" append-to-body>-->
<!--      <el-form ref="refuseForm" :model="refuseForm" :rules="refuseRules" label-width="80px">-->
<!--        <el-form-item label="拒绝原因" prop="reason">-->
<!--          <el-input type="textarea" v-model="refuseForm.reason" placeholder="请输入拒绝原因" />-->
<!--        </el-form-item>-->
<!--      </el-form>-->
<!--      <div slot="footer" class="dialog-footer" style="text-align: center">-->
<!--        <el-button type="primary" @click="refuseSubmit">确 定</el-button>-->
<!--        <el-button @click="cancel">取 消</el-button>-->
<!--      </div>-->
<!--    </el-dialog>-->
  </div>
</template>

<script>
import { listLogistics, getLogistics, delLogistics, addLogistics, updateLogistics, getCompany } from "@/api/system/logistics";
import { user } from "@/api/system/order";

export default {
  name: "Logistics",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物流单表格数据
      logisticsList: [],
      // 物流公司
      logisticsCompany: [],
      // 操作人、发货人
      user: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        logisticsNo: null,
        logisticsCompanyId: null,
        status: null,
        deliveryBy: null,
        deliveryTime: null,
        deliveryMinTime: null,
        deliveryMaxTime: null,
        createBy: null,
        createTime: null,
        createMinTime: null,
        createMaxTime: null,
      },
      // 表单参数
      form: {},

      //拒绝原因
      refuseForm: {
        reason: ''
      },
      refuseRules: {
        reason: [
          { required: true, message: "拒绝原因不能为空", trigger: "blur" }
        ]
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询物流单列表 */
    getList() {
      this.loading = true;
      listLogistics(this.queryParams).then(response => {
        this.logisticsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      user(this.user).then(response => {
        this.user = response.rows;
        console.log(this.user);
      });
      getCompany().then(response => {
        this.logisticsCompany = response.rows;
        console.log(this.logisticsCompany);
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        logisticsNo: null,
        logisticsCompanyId: null,
        senderName: null,
        senderTelephone: null,
        senderProvinceId: null,
        senderCityId: null,
        senderDistrictId: null,
        senderAddress: null,
        receiverName: null,
        receiverTelephone: null,
        receiverProvinceId: null,
        receiverCityId: null,
        receiverDistrictId: null,
        receiverAddress: null,
        totalWeight: null,
        totalVolume: null,
        totalAmount: null,
        status: null,
        description: null,
        deliveryBy: null,
        deliveryTime: null,
        createBy: null,
        createById: null,
        createTime: null,
        updateBy: null,
        updateById: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物流单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getLogistics(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改物流单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateLogistics(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLogistics(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除物流单编号为"' + ids + '"的数据项？').then(function() {
        return delLogistics(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/logistics/export', {
        ...this.queryParams
      }, `logistics_${new Date().getTime()}.xlsx`)
    },



    // 查看按钮
    selectPurchaseReturn(row) {
      const id = row.id || this.ids
      getLogistics(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "查看";
      });

      if (row.status==0){
        this.statusColor = "rgb(100, 100, 100)";
      }else if (row.status==3){
        this.statusColor = "rgb(82, 196, 26)";
      }else {
        this.statusColor = "rgb(255, 0, 0)";
      }
    },


    //拒绝原因
    refuseSubmit() {
      this.$refs["refuseForm"].validate(valid => {
        if (valid) {
          this.form.status=6;
          this.form.approveBy='1';
          this.form.refuseReason=this.refuseForm.reason
          this.form.approveTime=new Date();
          updateReceiveSheet(this.form).then(response => {
            this.$modal.msgError("审批拒绝");
            this.refuseOpen = false;
            this.open = false;
            this.getList();
          });
        }
      });
    },
  }
};
</script>
